package cn.ac.jaas.isc.vegetable.pbms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.ac.jaas.isc.vegetable.common.core.util.R;
import cn.ac.jaas.isc.vegetable.common.log.annotation.SysLog;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcEquipment;
import cn.ac.jaas.isc.vegetable.pbms.service.ArcEquipmentService;
import org.springframework.security.access.prepost.PreAuthorize;
import cn.ac.jaas.isc.vegetable.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 张高昌
 * @date 2022-05-02 11:14:42
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/arcequipment")
@Api(value = "arcequipment", tags = "设备管理")
public class ArcEquipmentController {

    private final ArcEquipmentService arcEquipmentService;

    /**
     * 分页查询
     *
     * @param page         分页对象
     * @param arcEquipment
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('pbms_arcequipment_view')")
    public R getArcEquipmentPage(Page page, ArcEquipment arcEquipment) {
        return R.ok(arcEquipmentService.page(page, Wrappers.query(arcEquipment)));
    }


    /**
     * 通过id查询
     *
     * @param equipmentId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{equipmentId}")
    @PreAuthorize("@pms.hasPermission('pbms_arcequipment_view')")
    public R getById(@PathVariable("equipmentId") Integer equipmentId) {
        return R.ok(arcEquipmentService.getById(equipmentId));
    }

    /**
     * 新增
     *
     * @param arcEquipment
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('pbms_arcequipment_add')")
    public R save(@RequestBody ArcEquipment arcEquipment, HttpServletRequest request) {
        return arcEquipmentService.saveEquipment(arcEquipment, request);
    }

    /**
     * 修改
     *
     * @param arcEquipment
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('pbms_arcequipment_edit')")
    public R updateById(@RequestBody ArcEquipment arcEquipment, HttpServletRequest request) {
        if (arcEquipment.getEquipmentId() == null) {
            R.failed("设备id不能为空！");
        }
        return arcEquipmentService.updateEquipmentById(arcEquipment, request);
    }

    /**
     * 通过id删除
     *
     * @param equipmentId id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除")
    @DeleteMapping("/{equipmentId}")
    @PreAuthorize("@pms.hasPermission('pbms_arcequipment_del')")
    public R removeById(@PathVariable Integer equipmentId) {
        return R.ok(arcEquipmentService.removeById(equipmentId));
    }


    /**
     * 导出excel 表格
     *
     * @param arcEquipment 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('pbms_arcequipment_export')")
    public List<ArcEquipment> export(ArcEquipment arcEquipment) {
        return arcEquipmentService.list(Wrappers.query(arcEquipment));
    }

    /**
     * 获取所有设备
     *
     * @return
     */
    @ApiOperation(value = "获取所有设备", notes = "获取所有设备")
    @GetMapping("/getEquipmentAll")
    @PreAuthorize("@pms.hasPermission('pbms_arcequipment_view')")
    public R getEquipmentAll() {

        return R.ok(arcEquipmentService.list());
    }

    /**
     * 分页查询历史修改设备
     *
     * @param equipmentId 设备id
     * @return R
     * @author 张高昌
     * @date 2022-07-41 17:41
     */
    @ApiOperation(value = "分页查询历史修改设备", notes = "分页查询历史修改设备")
    @GetMapping("/getHistoryPage")
    @PreAuthorize("@pms.hasPermission('pbms_arcequipment_view')")
    public R getHistoryPage(Page page, Integer equipmentId) {
        return R.ok(arcEquipmentService.getHistoryPage(page, equipmentId));
    }

    /**
     * 查询历史修改设备详情
     *
     * @param equipmentId
     * @return
     * @author 张高昌
     * @date 2022-08-08 10:12
     */
    @ApiOperation(value = "查询历史修改设备详情", notes = "查询历史修改设备详情")
    @GetMapping("/getHistoryInfo/{equipmentId}")
    @PreAuthorize("@pms.hasPermission('pbms_arcequipment_view')")
    public R getHistoryInfo(@PathVariable("equipmentId") Integer equipmentId) {
        return R.ok(arcEquipmentService.getById(equipmentId));
    }
}
